﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Common
{
    public static class Connections
    {
        public static Boolean SaveFile(string TenServer, string TenDangNhap, string MatKhau, string TenCoSoDuLieu)
        {
            Boolean check = false;
            try
            {
                string sFileName = @"\ConnectionSQLServer.xml";
                string Links = System.Windows.Forms.Application.StartupPath.ToString() + sFileName;
                if (Common.Utilities.CheckFile(sFileName) == true)
                {
                    Common.Utilities.DeleteFile(sFileName);
                }
                XmlTextWriter txtwrite = new XmlTextWriter(Links, null);
                txtwrite.WriteStartDocument();
                txtwrite.WriteStartElement("ConnectionSQL");
                txtwrite.WriteElementString("DataSource", Common.Utilities.Encrypt("SupermarketManagement", TenServer));
                txtwrite.WriteElementString("UserID", Common.Utilities.Encrypt("SupermarketManagement", TenDangNhap));
                txtwrite.WriteElementString("Password", Common.Utilities.Encrypt("SupermarketManagement", MatKhau));
                txtwrite.WriteElementString("InitialCatalog", Common.Utilities.Encrypt("SupermarketManagement", TenCoSoDuLieu));
                txtwrite.WriteEndElement();
                txtwrite.WriteEndDocument();
                txtwrite.Close();
                check = true;
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString(); check = false;
            }
            return check;
        }

        public static Boolean CreateFile()
        {
            Boolean kt = false;
            try
            {
                string sFileName = @"\ConnectionSQLServer.xml";
                string Links = System.Windows.Forms.Application.StartupPath.ToString() + sFileName;
                if (Common.Utilities.CheckFile(sFileName) == true)
                {
                    Common.Utilities.DeleteFile(sFileName);
                }
                XmlTextWriter txtwrite = new XmlTextWriter(Links, null);
                txtwrite.WriteStartDocument();
                txtwrite.WriteStartElement("ConnectionSQL");
                txtwrite.WriteElementString("DataSource", Common.Utilities.Encrypt("SupermarketManagement", "SERVER"));
                txtwrite.WriteElementString("UserID", Common.Utilities.Encrypt("SupermarketManagement", "sa"));
                txtwrite.WriteElementString("Password", Common.Utilities.Encrypt("SupermarketManagement", "123456"));
                txtwrite.WriteElementString("InitialCatalog", Common.Utilities.Encrypt("SupermarketManagement", "SupermarketManagement"));
                txtwrite.WriteEndElement();
                txtwrite.WriteEndDocument();
                txtwrite.Close();
                kt = true;
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                kt = false;
            }
            return kt;
        }

        public static Entities.SetupConnections GetConnection()
        {
            Entities.SetupConnections sql = new Entities.SetupConnections();
            string sFileName = @"\ConnectionSQLServer.xml";
            string Links = System.Windows.Forms.Application.StartupPath.ToString() + sFileName;
            try
            {
                XmlDocument doc = new XmlDocument();
                if (Common.Utilities.CheckFile(sFileName) == true)
                {
                    doc.Load(Links);
                    XmlElement root = doc.DocumentElement;
                    sql.DataSource = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[0].InnerText);
                    sql.UserID = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[1].InnerText);
                    sql.Passwords = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[2].InnerText);
                    sql.InitialCatalog = Common.Utilities.Decrypt("SupermarketManagement", root.ChildNodes[3].InnerText);
                }
                else
                {
                    CreateFile();
                    sql = null;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                Common.Utilities.DeleteFile(Links);
                CreateFile();
                sql = null;
            }
            return sql;
        }

        public static string GetConnectionString()
        {
            string con = null;
            try
            {
                con = System.Configuration.ConfigurationManager.ConnectionStrings["connectName"].ToString();
            }
            catch (Exception ex)
            { string s = ex.Message; con = null; }
            return con;
        }
    }
}
